package com.softserveinc.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Movie implements Parcelable {
	private static final int MAX_TITLE_SIZE = 30;

	private int id;
	private double popularity;
	private String title;
	private String posterHttpUrl;
	private String posterLocalUrl;
	private Date releaseDate;
	private Certification certification;
	private Bitmap poster;

	public Movie() {
	}

	public int getId() {
		return id;
	}

	public void setId(final int id) {
		this.id = id;
	}

	public double getPopularity() {
		return popularity;
	}

	public void setPopularity(final double popularity) {
		this.popularity = popularity;
	}

	public Date getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(final Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	public String getPosterLocalUrl() {
		return posterLocalUrl;
	}

	public void setPosterLocalUrl(final String posterLocalUrl) {
		this.posterLocalUrl = posterLocalUrl;
	}

	public String getPosterHttpUrl() {
		return posterHttpUrl;
	}

	public void setPosterHttpUrl(final String posterHttpUrl) {
		this.posterHttpUrl = posterHttpUrl;
	}

	public Certification getCertification() {
		return certification;
	}

	public void setCertification(final Certification certification) {
		this.certification = certification;
	}

	public Bitmap getPoster() {
		return poster;
	}

	public void setPoster(final Bitmap poster) {
		this.poster = poster;
	}

	@Override
	public String toString() {
		if (title.length() > MAX_TITLE_SIZE) {
			return (title.substring(0, MAX_TITLE_SIZE) + "...");
		}
		return title;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(final Parcel dest, final int flags) {
		Log.i("parcel", "write was called");

		dest.writeInt(id);
		dest.writeDouble(popularity);
		dest.writeString(posterHttpUrl);
		dest.writeString(title);
		dest.writeString(certification.toString());

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dest.writeString(dateFormat.format(releaseDate));

		dest.writeParcelable(poster, 0);
	}

	public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
		@Override
		public Movie createFromParcel(final Parcel in) {
			Movie movie = new Movie();

			Log.i("parcel", "I was called");

			movie.setId(in.readInt());
			movie.setPopularity(in.readDouble());
			movie.setPosterHttpUrl(in.readString());
			movie.setTitle(in.readString());
			movie.setCertification(Certification.getCertificationForName(in
					.readString()));

			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
			try {
				movie.setReleaseDate(dateFormat.parse(in.readString()));
			} catch (ParseException e) {
				movie.setReleaseDate(new Date());
			}

			movie.setPoster((Bitmap) in.readParcelable(null));
			return movie;
		}

		@Override
		public Movie[] newArray(final int size) {
			Log.i("parcel", "array was called");
			throw new UnsupportedOperationException();
		}
	};

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((certification == null) ? 0 : certification.hashCode());
		result = prime * result + id;
		long temp;
		temp = Double.doubleToLongBits(popularity);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((poster == null) ? 0 : poster.hashCode());
		result = prime * result
				+ ((posterHttpUrl == null) ? 0 : posterHttpUrl.hashCode());
		result = prime * result
				+ ((posterLocalUrl == null) ? 0 : posterLocalUrl.hashCode());
		result = prime * result
				+ ((releaseDate == null) ? 0 : releaseDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Movie other = (Movie) obj;

		return other.getId() == this.getId();
	}
}
